<template>
  <div class="page">
    <header class="hero">
      <div class="brand">cpolar Demo</div>
      <h1>Spring Boot 2 + Vue 3</h1>
      <p class="subtitle">
        仅前端通过 cpolar 暴露为 HTTPS 公网访问；后端本地运行，经由 Vite dev proxy 访问 <code>/api</code> 接口。
      </p>
      <div class="hero-actions">
        <a class="btn btn-secondary" href="https://www.cpolar.com" target="_blank" rel="noopener">cpolar 官网</a>
      </div>
    </header>

    <main class="container">
      <section class="card">
        <h2 class="card-title">后端接口响应 <span v-if="responseTimeMs != null" class="rt-note">（响应耗时：{{ responseTimeMs }} ms）</span></h2>
        <div class="kv kv-accent" :class="{ flash: justUpdated }">
          <div class="row">
            <div class="k">msg</div>
            <div class="v">{{ data.msg }}</div>
          </div>
          <div class="row">
            <div class="k">timestamp</div>
            <div class="v">
              <span v-if="justUpdated" class="live-dot" aria-hidden="true"></span>
              {{ data.timestamp }}
            </div>
          </div>
          <div class="row">
            <div class="k">status</div>
            <div class="v"><span class="badge" :class="statusClass">{{ data.status }}</span></div>
          </div>
          <div class="row">
            <div class="k">接口请求次数</div>
            <div class="v">{{ refreshCount }}</div>
          </div>
        </div>
        <div class="card-actions">
          <button class="btn" @click="fetchData" :disabled="loading">
            <span v-if="loading" class="spinner"></span>
            {{ loading ? '正在请求…' : '手动刷新' }}
          </button>
        </div>
        <div v-if="error" class="alert">{{ error }}</div>
      </section>

      <section class="card">
        <h2 class="card-title">项目简介</h2>
        <p>
          本项目用于演示：使用 cpolar 将本地前端（Vite 开发服务器）穿透到公网，后端维持本地运行，通过前端的代理访问。
        </p>
        <ul class="list">
          <li>前端本地端口：<code>5173</code>（Vite）</li>
          <li>后端本地端口：<code>8888</code>（Spring Boot 2）</li>
          <li>代理规则：<code>/api</code> → <code>http://localhost:8888</code></li>
          <li>示例接口：<code>/api/test</code>（返回 msg、timestamp、status、requestCount）</li>
        </ul>
        <p class="note">
          使用 cpolar 运行 <code>cpolar http 5173</code> 获取公网 <code>https</code> 域名，仅前端对外访问。
        </p>
      </section>
    </main>

    <footer class="footer">
      <span>© {{ currentYear }} cpolar demo</span>
    </footer>
  </div>
</template>

<script setup>
import { onMounted, reactive, ref, computed } from 'vue'
import { getTest } from './api'
import { toast } from './utils/toast'

const data = reactive({ msg: '-', timestamp: '-', status: '-' })
const error = ref('')
const loading = ref(false)
const refreshCount = ref(0)
const responseTimeMs = ref(null)
const justUpdated = ref(false)

const statusClass = computed(() => {
  if (data.status === 'success') return 'badge-success'
  if (data.status === '-') return 'badge-muted'
  return 'badge-warn'
})

const currentYear = new Date().getFullYear()

async function fetchData() {
  error.value = ''
  loading.value = true
  try {
    const start = performance.now()
    const res = await getTest()
    const end = performance.now()
    data.msg = res.msg
    data.timestamp = res.timestamp
    data.status = res.status
    responseTimeMs.value = Math.round(end - start)
    refreshCount.value = Number(res.requestCount ?? res.refreshCount ?? 0)
    // 触发一次更新高亮
    justUpdated.value = false
    requestAnimationFrame(() => { justUpdated.value = true })
    setTimeout(() => { justUpdated.value = false }, 900)
    toast.success('刷新成功')
  } catch (err) {
    error.value = err?.message || '请求失败，请确认后端已启动及代理配置正确。'
    toast.error(error.value)
  } finally {
    loading.value = false
  }
}

onMounted(() => {
  fetchData()
})
</script>

<style>
html, body, #app { height: 100%; }
* { box-sizing: border-box; }
:root {
  --bg: #0b1020;
  --bg-2: #0f172a;
  --card: #0b1229;
  --card-2: #0e1630;
  --text: #e5e7eb;
  --muted: #9aa3b2;
  --primary: #3b82f6;
  --primary-2: #60a5fa;
  --success: #16a34a;
  --warn: #f59e0b;
  --danger: #ef4444;
  --ring: rgba(59, 130, 246, .35);
}

.page {
  min-height: 100%;
  color: var(--text);
  background: radial-gradient(1200px 600px at 10% -10%, #1d2b4c 1%, transparent 60%),
              radial-gradient(1200px 800px at 120% 10%, #1a2544 0%, transparent 60%),
              linear-gradient(180deg, var(--bg), var(--bg-2));
}

.hero {
  text-align: center;
  padding: 44px 12px 18px;
}
.brand {
  display: inline-block;
  padding: 6px 10px;
  border-radius: 999px;
  background: linear-gradient(90deg, var(--primary), var(--primary-2));
  color: white;
  font-weight: 600;
  font-size: 12px;
  letter-spacing: .4px;
}
.hero h1 {
  margin: 10px 0 8px;
  font-size: 30px;
  line-height: 1.15;
}
.subtitle {
  margin: 0 auto;
  max-width: 820px;
  color: var(--muted);
  font-size: 14px;
}
.hero-actions { margin-top: 18px; }

.container {
  max-width: 960px;
  margin: 0 auto;
  padding: 0 14px 22px;
}

.card {
  margin: 12px 0;
  padding: 14px 14px 14px;
  background: linear-gradient(180deg, var(--card), var(--card-2));
  border: 1px solid rgba(255,255,255,.06);
  border-radius: 12px;
  box-shadow: 0 10px 24px rgba(0,0,0,.22), 0 1px 0 rgba(255,255,255,.04) inset;
}
.card-title {
  margin: 0 0 12px;
  font-size: 18px;
}

.kv { margin-top: 8px; }
.kv-accent { background: rgba(96,165,250,.10); border-radius: 10px; padding: 4px 8px; }
.row {
  display: grid;
  grid-template-columns: 120px 1fr;
  gap: 10px;
  padding: 8px 0;
  border-bottom: 1px dashed rgba(255,255,255,.06);
}
.row:last-child { border-bottom: none; }
.k { color: var(--muted); }
.v { word-break: break-all; }

.btn {
  appearance: none;
  border: none;
  outline: none;
  padding: 8px 14px;
  border-radius: 10px;
  color: white;
  background: linear-gradient(180deg, var(--primary), var(--primary-2));
  box-shadow: 0 6px 18px var(--ring);
  cursor: pointer;
  transition: transform .05s ease, filter .2s ease, box-shadow .2s ease;
}
.btn:hover { filter: brightness(1.05); }
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: .6; cursor: not-allowed; }
.btn-secondary {
  background: transparent;
  color: var(--primary-2);
  border: 1px solid rgba(96,165,250,.55);
  box-shadow: none;
}
.hero-actions .btn + .btn { margin-left: 10px; }

.spinner {
  width: 14px; height: 14px;
  border: 2px solid rgba(255,255,255,.5);
  border-top-color: white;
  border-radius: 50%;
  display: inline-block;
  vertical-align: -2px;
  margin-right: 8px;
  animation: spin .9s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.badge {
  display: inline-block;
  padding: 4px 8px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 600;
}
.badge-success { background: rgba(22,163,74,.15); color: #86efac; border: 1px solid rgba(22,163,74,.35); }
.badge-warn { background: rgba(245,158,11,.15); color: #fde68a; border: 1px solid rgba(245,158,11,.35); }
.badge-muted { background: rgba(148,163,184,.15); color: #cbd5e1; border: 1px solid rgba(148,163,184,.35); }

.alert {
  margin-top: 12px;
  padding: 10px 12px;
  border: 1px solid rgba(239,68,68,.35);
  background: rgba(239,68,68,.12);
  color: #fecaca;
  border-radius: 10px;
}

.list { margin: 8px 0 0 18px; color: var(--text); }
.list li { margin: 6px 0; }
.note { margin-top: 8px; color: var(--muted); }

.footer {
  text-align: center;
  padding: 16px 12px 20px;
  color: var(--muted);
}

.card-actions { margin-top: 10px; }

.rt-note { font-size: 12px; color: var(--muted); font-weight: 500; }

/* 刷新高亮与动态指示 */
.kv.flash .row { animation: flash-bg .9s ease; }
@keyframes flash-bg {
  0% { background: rgba(96,165,250,.18); }
  100% { background: transparent; }
}
.live-dot {
  display: inline-block;
  width: 8px; height: 8px;
  margin-right: 8px;
  border-radius: 50%;
  background: var(--primary-2);
  box-shadow: 0 0 0 0 rgba(96,165,250,.6);
  animation: pulse 1.4s ease-out 2;
  vertical-align: 1px;
}
@keyframes pulse {
  0% { box-shadow: 0 0 0 0 rgba(96,165,250,.7); }
  70% { box-shadow: 0 0 0 10px rgba(96,165,250, 0); }
  100% { box-shadow: 0 0 0 0 rgba(96,165,250, 0); }
}
</style>


